﻿using Nemesis.Plugins.NRelog.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nemesis.WoW.Tools;

namespace Nemesis.Plugins.NRelog.Controllers
{
    internal class PauseThread
    {
        Thread pauseThread;
        bool loggendIn = false;

        public PauseThread()
        {
            pauseThread = new Thread(new ThreadStart(Run));
            pauseThread.Start();
        }

        private void Run()
        {
            while (SchedulerSettings.isOnPause)
            {
                if (DateTime.Now > SchedulerSettings.restartTime)
                {
                    Logger.WriteLine("[NRelog] Relog time reached. We will relog in 5 seconds!");
                    Thread.Sleep(5000);
                    Logger.WriteLine("[NRelog] Starting relog process now!");
                    Logger.WriteLine("[NRelog] Login Frame is visible! Try to log in!");
                    while (!loggendIn)
                    {
                        if (Login.TryLoginToWoW(SchedulerSettings.bNet, SchedulerSettings.pw, SchedulerSettings.login, SchedulerSettings.realm, SchedulerSettings.character))
                        {
                            Logger.WriteLine("[NRelog] Relog successfull!");
                            Thread.Sleep(1000);
                            Logger.WriteLine("[NRelog] Restarting Bot in 2 seconds!");
                            Thread.Sleep(2000);
                            Bot.Engine.Resume();
                            SchedulerSettings.isOnPause = false;
                            loggendIn = true;
                            SchedulerSettings.timesReloged++;
                            Logger.WriteLine("[NRelog] We will relog " + (SchedulerSettings.relogCount - SchedulerSettings.timesReloged) + " more times!");
                        }
                    }
                    
                }
            }
        }
    }
}
